﻿using System;
using System.Linq;

/*
    This file is part of DiscontinuedChecker.

    DiscontinuedChecker is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

namespace Statistics
{
	public class Quartiles
	{
		public static double Median(IOrderedEnumerable<double> list)
		{
			return GetQuantile (list, 0.5);
		}

		public static double Mean(params double[] list)
		{
			return list.Sum () / ((double)list.Length);
		}

		public static double IQR(IOrderedEnumerable<double> list)
		{
			return ThirdQuartile (list) - FirstQuartile (list);
		}

		public static double FirstQuartile(IOrderedEnumerable<double> list)
		{
			return GetQuantile (list, 0.25);
		}
		public static double ThirdQuartile(IOrderedEnumerable<double> list)
		{
			return GetQuantile (list, 0.75);
		}
		public static double GetQuantile(IOrderedEnumerable<double> list, double quantile)
		{
			double[] array = list.ToArray ();
			return Mean (array [(int)Math.Floor ((quantile * (array.Length + 1) - 1))],
			             array [(int)Math.Ceiling ((quantile * (array.Length + 1) - 1))]);
		}
	}
}